package com.czk.bootlettuce.utils;

import com.czk.bootlettuce.consitant.FormNoConstant;
import com.czk.bootlettuce.consitant.FormNoTypeEnum;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/4 21:38
 */
public class FormNoSerialUtil {
    /**
     * 根据单据类型获取单号前缀
     *
     * @param formNoTypeEnum
     * @return 单号前缀
     */
    public static String getFormNoPrefix(FormNoTypeEnum formNoTypeEnum) {
        // 日期格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formNoTypeEnum.getDatePattern());
        return formNoTypeEnum.getPrefix() + dateTimeFormatter.format(LocalDateTime.now());
    }

    /**
     * 获取流水号缓存Key
     *
     * @param serialPrefix 流水号前缀
     * @return 流水号缓存Key
     */
    public static String getCacheKey(String serialPrefix) {
        return FormNoConstant.SERIAL_CACHE_PREFIX.concat(serialPrefix);
    }

    public static String completionSerial(String serialPrefix, Long incrementalSerial, FormNoTypeEnum formNoTypeEnum) {
        StringBuilder builder = new StringBuilder(serialPrefix);
        // 需要补0的长度=流水号长度 -当日自增计数长度
        int length = formNoTypeEnum.getSerialLength() - String.valueOf(incrementalSerial).length();
        for (int i = 0; i < length; i++) {
            // 补0
            builder.append("0");
        }
        // 当日子证书
        builder.append(incrementalSerial);
        return builder.toString();
    }
}
